/**
 * Copyright (c) Daniel Centore
 *
 * ex: set filetype=java expandtab tabstop=4 shiftwidth=4 :
 *
 * This program is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This code is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published
 * by the Free Software Foundation.
 */
package ronp.anim.gui;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import ronp.anim.data.filmstrip.Animation;
import ronp.anim.gui.tools.ObjectsMenu;
import ronp.anim.gui.tools.drawing.LineDrawer;
import ronp.anim.gui.tools.transforms.MoveTransform;
import ronp.anim.gui.viewer.AnimViewer;

public class KeyInputListener implements KeyListener
{
	public static final int KEY_LINE = KeyEvent.VK_L;
	public static final int KEY_MOVE = KeyEvent.VK_M;
	public static final int KEY_ESCAPE = KeyEvent.VK_ESCAPE;

	private AnimViewer viewer;
	private Animation anim;
	private SelectedState state;
	private ObjectsMenu menu;
	private Gui gui;

	public KeyInputListener(final AnimViewer viewer, final Animation anim, final SelectedState state, ObjectsMenu menu, Gui gui)
	{
		this.viewer = viewer;
		this.anim = anim;
		this.state = state;
		this.menu = menu;
		this.gui = gui;
	}

	@Override
	public void keyPressed(KeyEvent e)
	{
		int code = e.getKeyCode();

		switch (code)
		{
		case KEY_LINE:
			viewer.setTool(new LineDrawer(viewer, anim, state, gui.getUndoController()));
			break;

		case KEY_MOVE:
			viewer.setTool(new MoveTransform(viewer, anim, state, gui.getUndoController()));
			break;

		case KEY_ESCAPE:
			viewer.setTool(null);
			menu.getList().clearSelection();
			break;

		case KeyEvent.VK_Z:
			if (e.isControlDown())
				gui.getUndoController().undo();
			break;

		case KeyEvent.VK_Y:
			if (e.isControlDown())
				gui.getUndoController().redo();
			break;
		}
	}

	@Override
	public void keyReleased(KeyEvent e)
	{
		// ignore
	}

	@Override
	public void keyTyped(KeyEvent e)
	{
		// ignore
	}

}
